package com.mofang.powerdekorhelper.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.model.CreateOrder;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by luofei on 2017/7/17 0017.
 */

public class DialogUtils {

    private static DialogUtils dialogUtils;

    SweetAlertDialog pDialog;

    public static DialogUtils getInstance() {
        if (dialogUtils == null) {
            dialogUtils = new DialogUtils();
        }

        return dialogUtils;
    }

    public void createLoadingDialog(Context context, String loadInfo) {
        pDialog = new SweetAlertDialog(context, SweetAlertDialog.PROGRESS_TYPE);
        pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        pDialog.setTitleText(loadInfo);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    public boolean ispDialogShow() {

        if (pDialog != null) {
            if (pDialog.isShowing()) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    public void hideLoadingDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
        }
    }


    public void showUnderTextDialog(Context context) {
        new SweetAlertDialog(context)
                .setContentText("It's pretty, isn't it?")
                .show();
    }

    public void showErrorTextDialog(Context context) {
        new SweetAlertDialog(context, SweetAlertDialog.ERROR_TYPE)
                .setTitleText("Oops...")
                .setContentText("Something went wrong!")
                .show();
    }

    public void showSucessTextDialog(Context context) {
        new SweetAlertDialog(context, SweetAlertDialog.SUCCESS_TYPE)
                .setTitleText("Good job!")
                .setContentText("You clicked the button!")
                .show();
    }

    public void showWarningConfirmtDialog(Context context) {
        new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE)
                .setTitleText("Are you sure?")
                .setContentText("Won't be able to recover this file!")
                .setConfirmText("Yes,delete it!")
                .setCancelText("No,cancel plx!")
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sDialog) {
                        // reuse previous dialog instance
                        sDialog.setTitleText("Deleted!")
                                .setContentText("Your imaginary file has been deleted!")
                                .setConfirmText("OK")
                                .showCancelButton(false)
                                .setConfirmClickListener(null)
                                .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                    }
                })
                .show();
    }

    public void showCustomDialog(Context context, String title, String content, String sureText, String cancleText,
                                 SweetAlertDialog.OnSweetClickListener listener1, SweetAlertDialog.OnSweetClickListener listener2) {
        SweetAlertDialog customDialog = new SweetAlertDialog(context, SweetAlertDialog.CUSTOM_IMAGE_TYPE);
        customDialog.setCancelText(cancleText);
        customDialog.setConfirmText(sureText);
        customDialog.setConfirmClickListener(listener1);
        customDialog.setCancelClickListener(listener2);
        customDialog.setContentText(content);
        customDialog.setTitleText(title);
        customDialog.show();
    }

    /**
     * 分享的对话框
     */

    public static Dialog MySharedDialog(Context activity, View.OnClickListener clickListener) {

        View view = LayoutInflater.from(activity).inflate(R.layout.share_dialog_layout, null);

        view.findViewById(R.id.share_wechat_imv).setOnClickListener(clickListener);
        view.findViewById(R.id.share_moments_imv).setOnClickListener(clickListener);
        view.findViewById(R.id.share_sina_imv).setOnClickListener(clickListener);
        view.findViewById(R.id.share_cancle_tv).setOnClickListener(clickListener);

        Dialog myshareDialog = new Dialog(activity, R.style.loading_dialog);

        myshareDialog.setCanceledOnTouchOutside(true);

        LinearLayout.LayoutParams params = null;

        if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 横屏下
            params = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(activity) / 3, ScreenUtils.getScreenHeight(activity) / 3);
        } else {
            // 竖屏下
            params = new LinearLayout.LayoutParams(2 * ScreenUtils.getScreenWidth(activity) / 3, LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        myshareDialog.setContentView(view, params);

        return myshareDialog;
    }

    /**
     * 提交信息对话框
     */

    public static Dialog CommitInfoDialog(Context activity, final IgetCommitInfo getCommitInfo) {

        View view = LayoutInflater.from(activity).inflate(R.layout.commit_info_dialog_layout, null);
        final EditText editText = (EditText) view.findViewById(R.id.commit_dialog_edit);

        view.findViewById(R.id.commit_dialog_cancle_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getCommitInfo.cancleDialog();
            }
        });
        view.findViewById(R.id.commit_dialog_sure_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getCommitInfo.getCommitInfo(editText.getText().toString());
            }
        });

        Dialog commitDialog = new Dialog(activity, R.style.loading_dialog);

        commitDialog.setCanceledOnTouchOutside(false);

        LinearLayout.LayoutParams params = null;

        if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 横屏下
            params = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(activity) / 3, ScreenUtils.getScreenHeight(activity) / 3);
        } else {
            // 竖屏下
            params = new LinearLayout.LayoutParams(2 * ScreenUtils.getScreenWidth(activity) / 3, LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        commitDialog.setContentView(view, params);

        return commitDialog;
    }

    public interface IgetCommitInfo {

        void getCommitInfo(String commitInfo);

        void cancleDialog();
    }

    /**
     * 生成订单对话框
     */

    public static Dialog GenerateOrderDialog(Context activity, CreateOrder createOrder, final IGenerateOrder iGenerateOrder) {

        View view = LayoutInflater.from(activity).inflate(R.layout.generate_order_dialog_layout, null);

        final EditText orderEdit = (EditText) view.findViewById(R.id.generate_order_edit);
        final TextView nameTv = (TextView) view.findViewById(R.id.generate_order_name);
        final TextView phoneTv = (TextView) view.findViewById(R.id.generate_order_phone);

        if (createOrder != null) {
            orderEdit.setText(createOrder.getResult().getOrderBillNo());
            nameTv.setText(createOrder.getResult().getCustName());
            phoneTv.setText(createOrder.getResult().getCustMob());
        }

        view.findViewById(R.id.commit_dialog_cancle_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iGenerateOrder.cancleDialog();
            }
        });

        view.findViewById(R.id.commit_dialog_sure_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iGenerateOrder.getOrderCode(
                        orderEdit.getText().toString().trim(),
                        nameTv.getText().toString(),
                        phoneTv.getText().toString());
            }
        });

        view.findViewById(R.id.generate_order_refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iGenerateOrder.onRefresh(orderEdit.getText().toString());
            }
        });

        Dialog generateOrderDialog = new Dialog(activity, R.style.loading_dialog);
        generateOrderDialog.setCanceledOnTouchOutside(false);
        LinearLayout.LayoutParams params = null;

        if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 横屏下
            params = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(activity) / 3, ScreenUtils.getScreenHeight(activity) / 3);
        } else {
            // 竖屏下
            params = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(activity) - 200, LinearLayout.LayoutParams.WRAP_CONTENT);
        }

        generateOrderDialog.setContentView(view, params);

        return generateOrderDialog;
    }

    public interface IGenerateOrder {

        void getOrderCode(String orderCode, String name, String phone);

        void cancleDialog();

        void onRefresh(String orderCode);
    }

    /**
     * 报备对话框
     */

    public static Dialog ReportDialog(Context activity, final IReportInfo reportInfo) {

        View view = LayoutInflater.from(activity).inflate(R.layout.report_dialog_layout, null);
        final EditText nameEit = (EditText) view.findViewById(R.id.report_dialog_name_edit);
        final EditText phoneEdit = (EditText) view.findViewById(R.id.report_dialog_phone_edit);

        view.findViewById(R.id.commit_dialog_cancle_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reportInfo.cancleDialog();
            }
        });

        view.findViewById(R.id.commit_dialog_sure_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reportInfo.getReportInfo(nameEit.getText().toString(), phoneEdit.getText().toString());
            }
        });

        Dialog commitDialog = new Dialog(activity, R.style.loading_dialog);

        commitDialog.setCanceledOnTouchOutside(false);

        LinearLayout.LayoutParams params = null;

        if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 横屏下
            params = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(activity) / 3, ScreenUtils.getScreenHeight(activity) / 3);
        } else {
            // 竖屏下
            params = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(activity) - 200, LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        commitDialog.setContentView(view, params);

        return commitDialog;
    }

    public interface IReportInfo {

        void getReportInfo(String customerName, String customerPhone);

        void cancleDialog();
    }


    /**
     * 分享信息
     *
     * @param activity
     * @param shareInfo
     * @return
     */
    public static Dialog shareInfoDialog(Context activity, final IShareInfo shareInfo) {

        View view = LayoutInflater.from(activity).inflate(R.layout.share_info_dialog_layout, null);
        final EditText contentEit = (EditText) view.findViewById(R.id.share_info_edit);
        final TextView shareTv = (TextView) view.findViewById(R.id.share_info_sure_tv);

       /* view.findViewById(R.id.commit_dialog_cancle_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareInfo.cancleDialog();
            }
        });*/

        view.findViewById(R.id.share_info_sure_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                shareInfo.getShareInfo(contentEit.getText().toString());
            }
        });

        Dialog commitDialog = new Dialog(activity, R.style.loading_dialog);

        commitDialog.setCanceledOnTouchOutside(true);

        LinearLayout.LayoutParams params = null;

        if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 横屏下
            params = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(activity) / 3, ScreenUtils.getScreenHeight(activity) / 3);
        } else {
            // 竖屏下
            params = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(activity) - 200, LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        commitDialog.setContentView(view, params);

        return commitDialog;
    }

    public interface IShareInfo {

        void getShareInfo(String shareInfo);

        void cancleDialog();
    }

    public static Dialog chooseTimeDialog(Context activity, String title, View.OnClickListener listener) {

        View view = LayoutInflater.from(activity).inflate(R.layout.choose_time_dialog_layout, null);

        TextView titleTv = (TextView) view.findViewById(R.id.choose_time_title);
        titleTv.setText(title);
        view.findViewById(R.id.commit_dialog_cancle_imv).setOnClickListener(listener);
        view.findViewById(R.id.commit_dialog_undecided_tv).setOnClickListener(listener);
        view.findViewById(R.id.commit_dialog_sure_tv).setOnClickListener(listener);

        Dialog chooseTimeDialog = new Dialog(activity, R.style.loading_dialog);

        chooseTimeDialog.setCanceledOnTouchOutside(true);

        LinearLayout.LayoutParams params = null;

        if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 横屏下
            params = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(activity) / 3, ScreenUtils.getScreenHeight(activity) / 3);
        } else {
            // 竖屏下
            params = new LinearLayout.LayoutParams(2 * ScreenUtils.getScreenWidth(activity) / 3, LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        chooseTimeDialog.setContentView(view, params);

        return chooseTimeDialog;
    }


}
